package com.artogrid.framework.commons.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.Inflater;






public class ZipUtils {
	
	
	
	public static String gzip(String str) throws IOException {
		if (str == null || str.length() == 0) {
	      return str;
	    }
	    System.out.println(" string size="+str.length());
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    GZIPOutputStream gzip = new GZIPOutputStream(out);
	    gzip.write(str.getBytes());
	    gzip.close();
	    System.out.println("zip size="+out.size());
	    String strOut= out.toString("ISO-8859-1");
	    return strOut;
	}

	public static byte[] gzipToByteArray(byte[] bitArray) throws IOException {
		byte[] result=new byte[0];
		if (bitArray == null || bitArray.length == 0) {
	      return result;
	    }
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    GZIPOutputStream gzip = new GZIPOutputStream(out);
	    gzip.write(bitArray);
	    gzip.close();
//	    String strOut= out.toString("ISO-8859-1");
	    result=out.toByteArray();
	    return result;
	}
	
	
	public static byte[] gzipToByteArray(String str) throws IOException {
		byte[] result=new byte[0];
		if (str == null || str.length() == 0) {
	      return result;
	    }
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    GZIPOutputStream gzip = new GZIPOutputStream(out);
	    gzip.write(str.getBytes("UTF-8"));
	    gzip.close();
//	    String strOut= out.toString("ISO-8859-1");
	    result=out.toByteArray();
	    return result;
	}

	
	public static String ungzip(String str) throws IOException {
	    if (str == null || str.length() == 0) {
	      return str;
	    }
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    ByteArrayInputStream in = new ByteArrayInputStream(str
	        .getBytes("ISO-8859-1"));
	    GZIPInputStream gunzip = new GZIPInputStream(in);
	    byte[] buffer = new byte[256];
	    int n;
	    while ((n = gunzip.read(buffer)) >= 0) {
	      out.write(buffer, 0, n);
	    }
	    // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
	    return out.toString();
	}

	
	public static byte[] ungzip2ByteArray(byte[] stream) throws IOException {
		byte[] buffer = new byte[256];
		if (stream == null || stream.length == 0) {
	      return buffer;
	    }
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    ByteArrayInputStream in = new ByteArrayInputStream(stream);
	    GZIPInputStream gunzip = new GZIPInputStream(in);
	    
	    int n;
	    while ((n = gunzip.read(buffer)) >= 0) {
	      out.write(buffer, 0, n);
	    }
	    // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
	    return out.toByteArray();
	}
	
	public static String ungzip(byte[] stream) throws IOException {
	    if (stream == null || stream.length == 0) {
	      return "";
	    }
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    ByteArrayInputStream in = new ByteArrayInputStream(stream);
	    GZIPInputStream gunzip = new GZIPInputStream(in);
	    byte[] buffer = new byte[256];
	    int n;
	    while ((n = gunzip.read(buffer)) >= 0) {
	      out.write(buffer, 0, n);
	    }
	    // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
	    return out.toString();
	}
	
	public static int compressString(String data, byte[] output, int len) {     
		Deflater deflater = new Deflater();     
		deflater.setInput(data.getBytes(Charset.forName("utf-8")));     
		deflater.finish();     
		return deflater.deflate(output, 0, len); 
	}  
	public static String decompressString(byte[] input, int len) {      
		String result = null;     
		try {         
			Inflater inflater = new Inflater();         
			inflater.setInput(input, 0, len);          
			byte[] output = new byte[10000000]; 
			//todo may oveflow, find better solution         
			int resultLength = inflater.inflate(output);         
			inflater.end();          
			result = new String(output, 0, resultLength, Charset.forName("utf-8"));     
		} catch (DataFormatException e) {         
			// TODO Auto-generated catch block         
			e.printStackTrace();     
		}      
		return result; 
	} 

}
